﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyWrite.API.Appicant.Command.Holiday;
using MediatR;
using Microsoft.AspNetCore.Http;

namespace B.S.DataBase.MyWrite.API.Appicant.CommandHandler.Holiday
{
    public class UpdateHolidayCommandHarder:IRequestHandler<UpdateHolidayCommand, APIResult<object>>
    {
        private readonly IMapper mapper;
        private readonly IBaseRepository<Holidays> repository;

        public UpdateHolidayCommandHarder(IMapper mapper, IBaseRepository<Holidays> repository)
        {
            this.mapper = mapper;
            this.repository = repository;
        }

        public Task<APIResult<object>> Handle(UpdateHolidayCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            var getObj = repository.GetAll().Result.FirstOrDefault(x => x.HoId == request.HoId);
            getObj.HoName = request.HoName;
            getObj.DepId = request.DepId;
            getObj.HoBmId = request.HoBmId;
            getObj.HolbType = request.HolbType;
            getObj.HoTime = request.HoTime;
            getObj.TimeCount = request.TimeCount;
            getObj.BdaoTime = request.BdaoTime;
            getObj.Holyue = request.Holyue;
            getObj.Bmld = request.Bmld;
  
            //var model = mapper.Map<User>(request);
            var obj = repository.Update(getObj).Result;
            if (obj > 0)
            {
                result.Code = APIEnum.修改成功;
                result.Msg = APIEnum.修改成功.ToString();
                result.data = obj;
            }
            else
            {
                result.Code = APIEnum.修改失败;
                result.Msg = APIEnum.修改失败.ToString();
            }

            return Task.FromResult(result);
        }
    }
}
